package app.framework.freemarker.model;

import freemarker.template.TemplateMethodModel;
import freemarker.template.TemplateModelException;
import jasmine.framework.common.util.CheckUtil;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author mh.z
 */
public class PagePlaceholderMethodModel implements TemplateMethodModel {
    private Map<String, Object> placeholderValues;

    public PagePlaceholderMethodModel() {
        this.placeholderValues = Collections.emptyMap();
    }

    public PagePlaceholderMethodModel(Map<String, Object> placeholderValues) {
        this.placeholderValues = placeholderValues;
    }

    @Override
    public Object exec(List args) throws TemplateModelException {
        CheckUtil.notNull(args, "args null");

        if (args.size() < 1) {
            return "";
        }

        String placeholderKey = (String) args.get(0);
        Object placeholderValue = placeholderValues.get(placeholderKey);

        if (placeholderValue == null) {
            return "";
        }

        return placeholderValue;
    }

}
